﻿namespace OasisCiq.Xml
{
    using System;
    using System.Xml;
    using Xprl;

    public class XprlXmlWriter
    {
        private const string XmlNS = XmlNamespaces.PartyRelationships;
        private readonly XmlWriter writer;
        private readonly CommonTypesXmlWriter ctWriter;
        private readonly XpilXmlWriter xpilWriter;

        public XprlXmlWriter(XmlWriter writer)
        {
            if (writer == null)
            {
                throw new ArgumentNullException();
            }

            this.writer = writer;
            ctWriter = new CommonTypesXmlWriter(writer);
            xpilWriter = new XpilXmlWriter(writer);
        }

        public void WriteRelationship(Relationship element)
        {
            if (element == null || element.Party == null)
            {
                return;
            }

            writer.WriteStartElement("Relationship", XmlNS);
            if (element.RelationshipId != null)
            {
                writer.WriteAttributeString("RelationshipID", XmlNS, element.RelationshipId.ToString());
            }

            if (element.RelationshipIdType != null)
            {
                writer.WriteAttributeString("RelationshipIDType", XmlNS, element.RelationshipIdType.ToString());
            }

            if (element.Id != null)
            {
                writer.WriteAttributeString("ID", XmlNS, element.Id.ToString());
            }

            if (element.RelationshipType != null)
            {
                writer.WriteAttributeString("RelationshipType", XmlNS, element.RelationshipType.ToString());
            }

            if (element.RelationshipCategory != null)
            {
                writer.WriteAttributeString("RelationshipCategory", XmlNS, element.RelationshipCategory.ToString());
            }

            if (element.RelationshipStatus != null)
            {
                writer.WriteAttributeString("RelationshipStatus", XmlNS, element.RelationshipStatus.ToString());
            }

            if (element.RelationshipStartDateTime != null)
            {
                writer.WriteAttributeString("RelationshipStartDateTime", XmlNS, element.RelationshipStartDateTime.ToString());
            }

            if (element.RelationshipEndDateTime != null)
            {
                writer.WriteAttributeString("RelationshipEndDateTime", XmlNS, element.RelationshipEndDateTime.ToString());
            }

            if (element.RelationshipStartReason != null)
            {
                writer.WriteAttributeString("RelationshipStartReason", XmlNS, element.RelationshipStartReason.ToString());
            }

            if (element.RelationshipEndReason != null)
            {
                writer.WriteAttributeString("RelationshipEndReason", XmlNS, element.RelationshipEndReason.ToString());
            }

            if (element.RelationshipRelativityWithOtherParty != null)
            {
                writer.WriteAttributeString("RelationshipRelativityWithOtherParty", XmlNS, element.RelationshipRelativityWithOtherParty.ToString());
            }

            if (element.RelationshipRole != null)
            {
                writer.WriteAttributeString("RelationshipRole", XmlNS, element.RelationshipRole.ToString());
            }

            if (element.OtherPartyRelationshipRole != null)
            {
                writer.WriteAttributeString("OtherPartyRelationshipRole", XmlNS, element.OtherPartyRelationshipRole.ToString());
            }

            if (element.RelationshipPriority != null)
            {
                writer.WriteAttributeString("RelationshipPriority", XmlNS, element.RelationshipPriority.ToString());
            }

            ctWriter.WriteDataQualityGroup(element);
            ctWriter.WriteLanguageCodeGroup(element);
            ctWriter.WriteExtendedAttributes(element);
            WriteParty(element.Party);
            writer.WriteEndElement();
        }

        public void WriteParty(PartyType element)
        {
            if (element == null)
            {
                return;
            }

            xpilWriter.WritePartyStartElement(element, "Party", XmlNS);
            xpilWriter.WritePartyChildren(element);
            if (element.AdvancedRelationships != null)
            {
                foreach (var advancedRelationship in element.AdvancedRelationships)
                {
                    WriteRelationship(advancedRelationship);
                }
            }

            xpilWriter.WritePartyEndElement(element);
        }
    }
}
